package frankenstein.algo;
import java.awt.*;
import java.io.*;
import java.util.*;

import frankenstein.datastructures.Scenario;

public class ScenarioInterpreter {

	LinkedList<Scenario> scenarios = new LinkedList<Scenario>();
	
	public LinkedList<Scenario> makeScenarios(String scenarioFile){
		
		double probability = 1.0;
		boolean dyn = false;
		boolean stat = false;
		boolean semantic = false;
		boolean implement = false;
		
		File f = new File(scenarioFile);
		String line;
		try{
			FileInputStream fis = new FileInputStream(f);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader content = new BufferedReader(isr);

			while(content.ready()){
				
				line = content.readLine();
				if(!line.substring(0,2).equals("//")){
				//	System.out.println("subi " + line.substring(0,2));
					
					String[] stringParts = line.split(":");
			
					String description = stringParts[0];
					String resps = stringParts[1];
					String scenarioType = stringParts[2];
					
					String type = scenarioType.substring(0,1);
					try{
						String dynstat = scenarioType.substring(2,3);
						if(dynstat.equals("d")){
							dyn = true;
						}
						else if(dynstat.equals("s")){
							stat = true;
						}
					}
					catch(Exception e){
						
					}
					try{
						String implsem = scenarioType.substring(4,5);
						if(implsem.equals("i")){
							implement = true;
						}
						else if(implsem.equals("s")){
							semantic = true;
						}
					}
					catch(Exception e){
						
					}
					try{
						probability = Double.parseDouble(stringParts[3]);
					}
					catch(Exception e){
			
					}
					Scenario s = createScenario(description, resps, type, probability, dyn, stat, implement, semantic);
					scenarios.add(s);
				}
			}
		}
		catch(IOException ioe){
			System.out.println(ioe);
			//System.exit(0);
		}
		
		return scenarios;
		
	}
	
	public LinkedList<Scenario> makeNewScenarios(LinkedList<Scenario> scenariosList){
		LinkedList<Scenario> newScenarios = new LinkedList<Scenario>();
		
		for(int i = 0; i< scenariosList.size(); i++){
			Scenario currentScenario = scenariosList.get(i);
			Scenario s = createScenario(currentScenario.getScenarioDescription(),
					""+currentScenario.getResponsibility(),currentScenario.getType(),
					currentScenario.getScenarioProbability(),currentScenario.getDynamicEffect(),
					currentScenario.getStaticEffect(),currentScenario.getImplementationalChange(),currentScenario.getSemanticChange()
					);
			newScenarios.add(s);
			
		}
		
		System.out.println("new scenarios size"+newScenarios.size());
		
		return newScenarios;
	}
	public Scenario createScenario(String description, String resps, String scenarioType, double probability, boolean dynamicEffect, boolean staticEffect, boolean implement, boolean semantic){
		Scenario s = new Scenario();
		
		if(scenarioType.equals("Add"))
			scenarioType = "A";
		if(scenarioType.equals("Change"))
			scenarioType = "C";
		
		try{
		if(scenarioType.equals(Scenario.ADD)){
			
			Integer resp = Integer.parseInt(resps);
			
			s = new AddingScenario(description, resp, probability, dynamicEffect, staticEffect, implement, semantic);
			
		}
		else if(scenarioType.equals(Scenario.CHANGE)) {
			Integer resp = Integer.parseInt(resps);
			
			s = new ChangingScenario(description, resp, probability, dynamicEffect, staticEffect, implement, semantic);
			

		}
		}
		catch(Exception e){
			System.out.println("skenaarionluonti " + e);
		}
		return s;
	}
	
}
